/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ohos.request
import std.collection.*
import std.sync.*
import ohos.business_exception.*
import ohos.callback_invoke.*
import ohos.base
import ohos.ffi.*
import ohos.labels.*
import std.deriving.Derive
import ohos.app.ability.ui_ability.{UIAbilityContext, getStageContext}

import ohos.base.{NativeOptionCString, NativeOptionInt64, NativeOptionUInt32}
import ohos.business_exception.BusinessException

/**
* Event CallbackType.
*/
@Derive[ToString, Hashable, Equatable]
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public enum EventCallbackType {
    /**
    * Progress
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Progress |
    /**
    * Completed
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Completed |
    /**
    * Failed
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Failed |
    /**
    * Pause
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Pause |
    /**
    * Resume
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Resume |
    /**
    * Remove
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Remove |
    /**
    * Response
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Response |
    ...
}



/**
* The action options.
*
* @relation enum Action
*/
@Derive[ToString, Equatable]
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public enum Action {
    /**
    * Indicates download task.
    *
    * @relation DOWNLOAD = 0
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Download |
    /**
    * Indicates upload task.
    *
    * @relation UPLOAD = 1
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Upload |
    ...
}



/**
* The mode options.
*
* @relation enum Mode
*/
@Derive[ToString, Equatable]
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public enum Mode {
    /**
    * Indicates background task.
    *
    * @relation BACKGROUND = 0
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Background |
    /**
    * Indicates foreground task.
    *
    * @relation FOREGROUND = 1
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Foreground |
    ...
}



/**
* The network options.
*
* @relation enum Network
*/
@Derive[ToString, Equatable]
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public enum Network {
    /**
    * Indicates no restriction on network type.
    *
    * @relation ANY = 0
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    AnyType |
    /**
    * Indicates Wi-Fi only.
    *
    * @relation WIFI = 1
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Wifi |
    /**
    * Indicates cellular only.
    *
    * @relation CELLULAR = 2
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Cellular |
    ...
}



/**
* Broadcast events for the request.
*
* @relation enum BroadcastEvent
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public enum BroadcastEvent <: ToString {
    /**
    * Completion event for the task.
    *
    * @relation COMPLETE = 'ohos.request.event.COMPLETE'
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Complete |
    ...
    /**
    * Gets the corresponding mapping value.
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public func toString(): String
}


/**
* The file information for a form item.
*
* @relation interface FileSpec
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public class FileSpec {
    /**
    * The path to save the uploaded file.
    * Currently support:
    * 1: relative path, like "./xxx/yyy/zzz.html", "xxx/yyy/zzz.html", under caller's cache folder.
    * 2: internal protocol path, starting with "internal://", like "internal://cache/path/to/file.txt".
    * 3: application storage path, only the base directory and its subdirectories are supported, like "/data/storage/el1/base/path/to/file.txt".
    * 4: file protocol path with self bundle name, only the base directory and its subdirectories are supported, like "file://com.example.test/data/storage/el2/base/file.txt".
    * 5: user file url, like "file://media/Photo/path/to/file.png".
    *
    * @relation path: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var path: String
    
    /*
    * The MIME type of the file.
    * The default is obtained by the suffix of the filename.
    *
    * @relation mimeType?: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var mimeType: ?String
    
    /*
    * The filename, the default is obtained by path.
    *
    * @relation filename?: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var filename: ?String
    
    /*
    * The extras for the file information.
    *
    * @relation extras?: object
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var extras: HashMap<String, String>
    
    /**
    * FileSpec constructor.
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public init(
        path: String,
        mimeType!: ?String = None,
        filename!: ?String = None,
        extras!: HashMap<String, String> = HashMap<String, String>()
    )
}


/**
* FormItem value
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public enum FormItemValue {
    /**
    * String type FormItem value
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    StringItem(String) |
    /**
    * FileSpec type FormItem value
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    FileItem(FileSpec) |
    /**
    * Array<FileSpec> type FormItem value
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    FileItemArray(Array<FileSpec>) |
    ...
}


/**
* The form item information for a task.
*
* @relation interface FormItem
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public class FormItem {
    /*
    * The item's name.
    *
    * @relation name: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var name: String
    
    /*
    * The item's value.
    *
    * @relation value: string | FileSpec | Array<FileSpec>
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var value: FormItemValue
    
    /**
    * FormItem constructor.
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public init(name: String, value: FormItemValue)
}


/**
* Config data
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public enum ConfigData {
    /**
    * String type Config data
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    StringValue(String) |
    /**
    * Array<FormItem> type Config data
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    FormItems(Array<FormItem>) |
    ...
}


/**
* The configurations for a task.
* Provides the configuration information of an upload or download task.
* Using a flexible configuration for clear upload and download functions.
* If without emphasis, an option is for any task.
*
* @relation interface Config
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public class Config {
    /**
    * The Universal Resource Locator for a task.
    * The maximum length is 2048 characters.
    * Using raw `url` option, even url parameters in it.
    *
    * @relation action: Action
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var action: Action
    
    /**
    * The Universal Resource Locator for a task.
    * The maximum length is 2048 characters.
    * Using raw `url` option, even url parameters in it.
    *
    * @relation url: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var url: String
    
    /**
    * The title for a task, give a meaningful title please.
    * The maximum length is 256 characters.
    * The default is upload or download, consistent with its action.
    *
    * @relation title?: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var title: ?String
    
    /**
    * The details for a task.
    * The maximum length is 1024 characters.
    * The default is empty string.
    *
    * @relation description?: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var description: String
    
    /**
    * Indicates task's mode.
    * The default is Background.
    * For frontend task, it has callbacks.
    * For background task, it has notifications and fallback.
    * The cross-platform default is Foreground.
    *
    * @relation mode?: Mode
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var mode: Mode
    
    /**
    * The solution choice when path already exists during download.
    * The default is false.
    * Currently support:
    * true, rewrite the existed file;
    * false, go to fail.
    *
    * @relation overwrite?: boolean
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var overwrite: Bool
    
    /**
    * The HTTP standard method for upload or download: GET/POST/PUT.
    * Case insensitive.
    * For upload, use PUT/POST, the default is PUT.
    * For download, use GET/POST, the default is GET.
    *
    * @relation method?: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var method: ?String
    
    /**
    * The HTTP headers.
    * For upload request, the `Content-Type` is forced to `multipart/form-data`.
    * For download request, the default `Content-Type` is `application/json`.
    *
    * @relation headers?: object
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var headers: HashMap<String, String>
    
    /**
    * The arguments, it can be any text, uses json usually.
    * For download, it can be raw string, the default is empty string.
    * For upload, it can be form items, the default is a empty form.
    * There must be one `FileSpec` item at least or will be a parameter error.
    *
    * @relation data?: string | Array<FormItem>
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var data: ?ConfigData
    
    /**
    * The path to save the downloaded file, the default is "./".
    * Currently support:
    * 1: relative path, like "./xxx/yyy/zzz.html", "xxx/yyy/zzz.html", under caller's cache folder.
    * 2: internal protocol path, starting with "internal://", like "internal://cache/path/to/file.txt".
    * 3: application storage path, only the base directory and its subdirectories are supported, like "/data/storage/el1/base/path/to/file.txt".
    * 4: file protocol path with self bundle name, only the base directory and its subdirectories are supported, like "file://com.example.test/data/storage/el2/base/file.txt".
    *
    * @relation saveas?: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var saveas: String
    
    /**
    * Network used for the task.
    * The default value is Any (Wi-Fi or cellular).
    *
    * @relation network?: Network
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var network: Network
    
    /**
    * Allows work in metered network or not.
    * The default is false
    *
    * @relation metered?: boolean
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var metered: Bool
    
    /**
    * Allows work in roaming network or not.
    * The default is true.
    *
    * @relation roaming?: boolean
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var roaming: Bool
    
    /**
    * Enable automatic retry or not for the background task.
    * The frontend task is always fast-fail.
    *
    * @relation retry?: boolean
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var retry: Bool
    
    /**
    * Allows redirect or not.
    * The default is true.
    *
    * @relation redirect?: boolean
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var redirect: Bool
    
    /**
    * The index of paths for a task.
    * Usually used for a continuous job.
    * The default is 0.
    *
    * @relation index?: number
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var index: UInt32
    
    /**
    * The start point of a file.
    * Usually used for a continuous job.
    * It will set the "Range" header in download.
    * It will start read at the point in upload.
    * The default is 0.
    *
    * @relation begins?: number
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var begins: Int64
    
    /**
    * The end point of a file.
    * Usually used for a continuous job.
    * It will set The "Range" header in download.
    * It will end read at the point in upload.
    * The default is -1 indicating the end of the data for upload or download.
    *
    * @relation ends?: number
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var ends: Int64
    
    /**
    * The policy of the progress notification for background task.
    * If false: only completed or failed notification, the default.
    * If true, emits every progress, completed or failed notifications.
    *
    * @relation gauge?: boolean
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var gauge: Bool
    
    /**
    * Breaks when fail to fetch filesize before upload/download or not.
    * Uses filesize for a precise gauge.
    * The default is false, set size as -1 indicating the case.
    *
    * @relation precise?: boolean
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var precise: Bool
    
    /**
    * For in-application layer isolation.
    * If given:
    *   the minimum is 8 bytes.
    *   the maximum is 2048 bytes.
    * Creates a task with token, then must provide it during normal query.
    * So saves the token carefully, it can not be retrieved by query.
    * Or leave it empty.
    *
    * @relation token?: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var token: ?String
    
    /**
    * The priority of this task.
    * Front-end tasks have higher priority than back-end tasks.
    * In tasks of the same mode, the smaller the number, the higher the priority.
    * The default is 0.
    *
    * @relation priority?: number
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var priority: UInt32
    
    /**
    * The extras for the configuration.
    * This parameter is left empty by default.
    *
    * @relation extras?: object
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var extras: HashMap<String, String>
    
    /**
    * Config constructor.
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public init(action: Action, url: String, title!: ?String = None, description!: String = "",
        mode!: Mode = Mode.Background, overwrite!: Bool = false, method!: ?String = None,
        headers!: HashMap<String, String> = HashMap<String, String>(), data!: ?ConfigData = None, saveas!: String = "./",
        network!: Network = Network.AnyType, metered!: Bool = false, roaming!: Bool = true, retry!: Bool = true,
        redirect!: Bool = true, index!: UInt32 = 0, begins!: Int64 = 0, ends!: Int64 = -1, gauge!: Bool = false,
        precise!: Bool = false, token!: ?String = None, priority!: UInt32 = 0,extras!: HashMap<String, String> = HashMap<String, String>()
    )
}


/**
* Indicate the current state of the task.
*
* @relation enum State
*/
@Derive[ToString]
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public enum State {
    /**
    * Indicates a task created by `new Task(Config)`.
    *
    * @relation INITIALIZED = 0x00
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Initialized |
    /**
    * Indicates a task lack of resources or conditions to run or retry.
    *
    * @relation WAITING = 0x10
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Waiting |
    /**
    * Indicates a task in processing now.
    *
    * @relation RUNNING = 0x20
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Running |
    /**
    * Indicates a task failed once at least and in processing again now.
    *
    * @relation RETRYING = 0x21
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Retrying |
    /**
    * Indicates a paused task which tends to be resumed for continuous work.
    *
    * @relation PAUSED = 0x30
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Paused |
    /**
    * Indicates a stopped task which must be started again.
    *
    * @relation STOPPED = 0x31
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Stopped |
    /**
    * Indicates a completed task which finish its data transfer.
    *
    * @relation COMPLETED = 0x40
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Completed |
    /**
    * Indicates a failed task which interrupted by some error.
    *
    * @relation FAILED = 0x41
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Failed |
    /**
    * Indicates a removed task which can not be processed again.
    *
    * @relation REMOVED = 0x50
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Removed |
    ...
}



/**
* The progress data structure.
* Upload allows multiple files per upload task.
* Only one file in a download task.
* So using a unified data structure for progress.
* Generally:
* 1: sum(sizes) is total files size of the task.
* 2: float(processed)/sizes[counter] is the progress for the current processing file.
* 3: float(sum(sizes[:index])+processed)/sum(sizes) is the summary progress for a task.
* If fetch file size in failure, the size of the file in sizes will be set as -1.
*
* @relation interface Progress
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public class Progress {
    /**
    * The current state of the task.
    *
    * @relation readonly state: State
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let state: State
    
    /**
    * The current processing file index in a task.
    *
    * @relation readonly index: number
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let index: UInt32
    
    /**
    * The processed data size for the current file in a task.
    *
    * @relation readonly processed: number
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let processed: Int64
    
    /**
    * The sizes of files in a task, in bytes.
    *
    * @relation readonly sizes: Array<number>
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let sizes: Array<Int64>
    
    /**
    * The extras for an interaction.
    * Such as headers and body of response from server.
    * But when the Content-Disposition header responded, the body will be into the uri of its attachment only, the body here is empty.
    * {"headers": {"key": v}, "body": "contents"}.
    * The "body" field is not supported in cross-platform scenarios.
    *
    * @relation readonly extras?: object
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let extras: HashMap<String, String>
}


/**
* The HTTP response.
* Describes the data structure of the task response header.
*
* @relation interface HttpResponse
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public class HttpResponse {
    /**
    * The version of the HTTP response.
    *
    * @relation readonly version: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let version: String
    
    /**
    * The status code of the HTTP response.
    *
    * @relation readonly statusCode: number
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let statusCode: Int32
    
    /**
    * The reason of the HTTP response.
    *
    * @relation readonly reason: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let reason: String
    
    /**
    * The headers of the HTTP response.
    *
    * @relation readonly headers: Map<string, Array<string>>
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let headers: HashMap<String, Array<String>>
}


/**
* Indicates the reason for the failure.
*
* @relation enum Faults
*/
@Derive[ToString]
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public enum Faults {
    /**
    * Indicates others failure.
    *
    * @relation OTHERS = 0xFF
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Others |
    /**
    * Indicates network disconnection.
    *
    * @relation DISCONNECTED = 0x00
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Disconnected |
    /**
    * Indicates task timeout.
    *
    * @relation TIMEOUT = 0x10
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Timeout |
    /**
    * Indicates protocol error, such as 5xx response from server.
    *
    * @relation PROTOCOL = 0x20
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Protocol |
    /**
    * Indicates filesystem io error, such as open/seek/read/write/close.
    *
    * @relation FSIO = 0x40
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    Fsio |
    ...
}



/**
* The filter data structure.
* Used for search, given fields works as **LOGICAL AND**.
* Invalid value may cause a parameter error.
*
* @relation interface Filter
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public class Filter {
    /**
    * Specify the end Unix timestamp.
    * The default is the moment of calling.
    *
    * @relation before?: number
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var before: ?Int64
    
    /**
    * Specify the start Unix timestamp.
    * The default is "`before` - 24 hours".
    *
    * @relation after?: number
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var after: ?Int64
    
    /**
    * Specify the state of tasks.
    * The default is any state.
    *
    * @relation state?: State
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var state: ?State
    
    /**
    * Specify the action of tasks, "upload" or "download", case insensitive.
    * The default is upload and download.
    *
    * @relation action?: Action
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var action: ?Action
    
    /**
    * Specify task's mode.
    * The default is Foreground and Background.
    *
    * @relation mode?: Mode
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var mode: ?Mode
    
    /**
    * Filter constructor.
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public init(before!: ?Int64 = None, after!: ?Int64 = None, state!: ?State = None,
        action!: ?Action = None, mode!: ?Mode = None
    )
}


/**
* The task information data structure for query results.
* Provides common query and advanced query, visible range of fields is different.
*
* @relation interface TaskInfo
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public class TaskInfo {
    /**
    * The path to save the downloaded file.
    *
    * @relation readonly saveas?: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let saveas: String
    
    /**
    * The url of a task.
    * For `${ show }` and `${ touch }`.
    *
    * @relation readonly url?: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let url: String
    
    /**
    * The arguments.
    * For `${ show }` and `${ touch }`.
    *
    * @relation readonly data?: string | Array<FormItem>
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let data: ConfigData
    
    /**
    * The task id.
    *
    * @relation readonly tid: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let tid: String
    
    /**
    * The task title.
    *
    * @relation readonly title: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let title: String
    
    /**
    * The task details.
    *
    * @relation readonly description: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let description: String
    
    /**
    * The task action.
    *
    * @relation readonly action: Action
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let action: Action
    
    /**
    * Specify task mode.
    * The default is frontend
    *
    * @relation readonly mode: Mode
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let mode: Mode
    
    /**
    * The priority of this task.
    * Front-end tasks have higher priority than back-end tasks.
    * In tasks of the same mode, the smaller the number, the higher the priority.
    * The default is 0.
    *
    * @relation readonly priority: number
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let priority: UInt32
    
    /**
    * The MIME type in the configuration of the task.
    *
    * @relation readonly mimeType: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let mimeType: String
    
    /**
    * An instance of `Progress` for a task.
    *
    * @relation readonly progress: Progress
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let progress: Progress
    
    /**
    * The progress notification policy of a background task.
    *
    * @relation readonly gauge: boolean
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let gauge: Bool
    
    /**
    * The creating date and time of a task in Unix timestamp.
    * It is generated by system of current device.
    *
    * @relation readonly ctime: number
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let ctime: UInt64
    
    /**
    * The modified date and time of a task in Unix timestamp.
    * It is generated by system of current device.
    *
    * @relation readonly mtime: number
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let mtime: UInt64
    
    /**
    * The retry switch of a task.
    * Just for background, frontend always disable
    *
    * @relation readonly retry: boolean
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let retry: Bool
    
    /**
    * The tried times of a task.
    *
    * @relation readonly tries: number
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let tries: UInt32
    
    /**
    * The faults case of a task.
    *
    * @relation readonly faults: Faults
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let faults: Faults
    
    /**
    * The reason of a waiting/failed/stopped/paused task.
    *
    * @relation readonly reason: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let reason: String
    
    /**
    * The extras of a task.
    * For frontend, nothing now.
    *
    * @relation readonly extras?: object
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let extras: HashMap<String, String>
}


/**
* The task entry.
* New task' status is "initialized" and enqueue.
* Can `start` a initialized task.
* Can `pause` a waiting/running/retrying background task.
* Can `resume` a paused background task.
* Can `stop` a running/waiting/retrying task and dequeue it.
*
* @relation interface Task
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public class Task {
    /**
    * The task id, unique on system.
    * Generated automatically by system.
    *
    * @relation readonly tid: string
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public let tid: String
    
    /**
    * The configurations for the task.
    *
    * @relation config: Config
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public var config: Config
    
    /**
    * Task constructor.
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public init(tid: String, config: Config)
    
    /**
    * Enables the response callback.
    * Subscribes to task response headers.
    *
    * @param { String } event - event types.
    * @param { Callback1Argument<HttpResponse> } callback - callback function with an `HttpResponse` argument.
    * @throws { BusinessException } 401 - Parameter verification failed.
    * @relation on(event: 'response', callback: Callback<HttpResponse>): void
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public func on(event: EventCallbackType, callback: Callback1Argument<HttpResponse>): Unit
    
    /**
    * Enables the specified callback.
    * Subscribes to task progress changes, completion events, failure events, pause events, resume events or removal events.
    *
    * @param { String } event - event types.
    * @param { Callback1Argument<Progress> } callback - callback function with a `Progress` argument.
    * @throws { BusinessException } 401 - Parameter verification failed.
    * @relation on(event: 'progress', callback: (progress: Progress) => void): void
    * @relation on(event: 'completed', callback: (progress: Progress) => void): void
    * @relation on(event: 'failed', callback: (progress: Progress) => void): void
    * @relation on(event: 'pause', callback: (progress: Progress) => void): void
    * @relation on(event: 'resume', callback: (progress: Progress) => void): void
    * @relation on(event: 'remove', callback: (progress: Progress) => void): void
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public func on(event: EventCallbackType, callback: Callback1Argument<Progress>): Unit
    
    /**
    * Disables the specified callback.
    * Unsubscribes task progress changes, completion events, failure events, pause events, resume events or removal events.
    *
    * @param { String } event - event types.
    * @param { CallbackObject } callback - callback function.
    * @relation off(event: 'progress', callback?: (progress: Progress) => void): void
    * @relation off(event: 'completed', callback?: (progress: Progress) => void): void
    * @relation off(event: 'failed', callback?: (progress: Progress) => void): void
    * @relation off(event: 'pause', callback?: (progress: Progress) => void): void
    * @relation off(event: 'resume', callback?: (progress: Progress) => void): void
    * @relation off(event: 'remove', callback?: (progress: Progress) => void): void
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public func off(event: EventCallbackType, callback!: ?CallbackObject = None): Unit
    
    /**
    * Starts the task. The following tasks can be started:
    * 1. Tasks just created.
    * 2. Download tasks that are stopped or become FAILED.
    *
    * @throws { BusinessException } 201 - Permission denied.
    * @throws { BusinessException } 13400003 - Task service ability error.
    * @throws { BusinessException } 21900007 - Operation with wrong task state.
    * @relation start(): Promise<void>
    */
    @!APILevel[
        22,
        permission: "ohos.permission.INTERNET",
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public func start(): Unit
    
    /**
    * Pauses the background task.
    *
    * @throws { BusinessException } 13400003 - Task service ability error.
    * @throws { BusinessException } 21900005 - Operation with wrong task mode.
    * @throws { BusinessException } 21900007 - Operation with wrong task state.
    * @relation pause(): Promise<void>
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public func pause(): Unit
    
    /**
    * Resumes the background task.
    *
    * @throws { BusinessException } 201 - Permission denied.
    * @throws { BusinessException } 13400003 - Task service ability error.
    * @throws { BusinessException } 21900005 - Operation with wrong task mode.
    * @throws { BusinessException } 21900007 - Operation with wrong task state.
    * @relation resume(): Promise<void>
    */
    @!APILevel[
        22,
        permission: "ohos.permission.INTERNET",
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public func resume(): Unit
    
    /**
    * Stop a running, waiting, or retrying task.
    *
    * @throws { BusinessException } 13400003 - Task service ability error.
    * @throws { BusinessException } 21900007 - Operation with wrong task state.
    * @relation stop(): Promise<void>
    */
    @!APILevel[
        22,
        syscap: "SystemCapability.Request.FileTransferAgent"
    ]
    public func stop(): Unit
}



/**
* Creates a task for upload or download and enqueue it.
* Only foreground application can create the frontend task.
* It can deal only one frontend task at a time.
* A in processing frontend task will be forced to stop when its application had switched to background.
* A new frontend task will interrupt a existed in processing frontend task.
* The background task is highly recommended.
*
* @param { UIAbilityContext } context - context of the caller.
* @param { Config } config - configurations for a task.
* @returns { Task } the upload or download task.
* @throws { BusinessException } 201 - Permission denied.
* @throws { BusinessException } 401 - Parameter error. Possible causes: 1. Missing mandatory parameters.
* 2. Incorrect parameter type. 3. Parameter verification failed.
* @throws { BusinessException } 13400001 - Invalid file or file system error.
* @throws { BusinessException } 13400003 - Task service ability error.
* @throws { BusinessException } 21900004 - the application task queue is full.
* @throws { BusinessException } 21900005 - Operation with wrong task mode.
* @throws { IllegalArgumentException } - The context is invalid.
*
* @relation function create(context: BaseContext, config: Config): Promise<Task>
*/
@!APILevel[
    22,
    permission: "ohos.permission.INTERNET",
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public func create(context: UIAbilityContext, config: Config): Task


/**
* Removes specified task belongs to the caller.
* The task will be forced to stop if in processing.
*
* @param { String } id - the task id.
* @throws { BusinessException } 401 - Parameter error. Possible causes: 1. Missing mandatory parameters.
* 2. Incorrect parameter type.
* @throws { BusinessException } 13400003 - Task service ability error.
* @throws { BusinessException } 21900006 - Task removed or not found.
* @relation function remove(id: string): Promise<void>
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public func remove(id: String): Unit


/**
* Gets the task with the specified id.
* Obtains task information based on the task ID.
*
* @param { UIAbilityContext } context - context of the caller.
* @param { String } id - the id of the task.
* @param { String } token - the token of the task, length between 8 and 2048 bytes.
* @throws { BusinessException } 401 - Parameter error. Possible causes: 1. Missing mandatory parameters.
* 2. Incorrect parameter type. 3. Parameter verification failed.
* @throws { BusinessException } 13400003 - Task service ability error.
* @throws { BusinessException } 21900006 - Task removed or not found.
* @throws { IllegalArgumentException } - The context is invalid.
*
* @relation function getTask(context: BaseContext, id: string, token?: string): Promise<Task>
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public func getTask(context: UIAbilityContext, id: String, token!: ?String = None): Task


/**
* Shows specified task details belongs to the caller.
* Queries a task details based on the task ID.
*
* @param { String } id - the task id.
* @returns { TaskInfo } a task info base on the task ID
* @throws { BusinessException } 401 - Parameter error. Possible causes: 1. Missing mandatory parameters.
* 2. Incorrect parameter type.
* @throws { BusinessException } 13400003 - Task service ability error.
* @throws { BusinessException } 21900006 - Task removed or not found.
* @relation function show(id: string): Promise<TaskInfo>
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public func show(id: String): TaskInfo


/**
* Touches specified task with token.
* Queries the task details based on the task ID and token.
*
* @param { String } id - the task id.
* @param { String } token - the in-application isolation key.
* @returns { TaskInfo } a task info base on the task ID and token
* @throws { BusinessException } 401 - Parameter error. Possible causes: 1. Missing mandatory parameters.
* 2. Incorrect parameter type.
* @throws { BusinessException } 13400003 - Task service ability error.
* @throws { BusinessException } 21900006 - Task removed or not found.
* @relation function touch(id: string, token: string): Promise<TaskInfo>
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public func touch(id: String, token: String): TaskInfo


/**
* Searches tasks, for system.
* Searches for task IDs based on Filter.
*
* @param { Filter } filter - an instance of `Filter`.
* @returns { Array<String> } task ids which matches filter
* @throws { BusinessException } 401 - Parameter error. Possible causes: 1. Missing mandatory parameters.
* 2. Incorrect parameter type.
* @throws { BusinessException } 13400003 - Task service ability error.
*
* @relation function search(filter?: Filter): Promise<Array<string>>
*/
@!APILevel[
    22,
    syscap: "SystemCapability.Request.FileTransferAgent"
]
public func search(filter!: Filter = Filter()): Array<String>


